package ink.xiaobaibai.serviceOxy;

import ink.xiaobaibai.bo.DeviceMqttBo;
import ink.xiaobaibai.common.DeviceType;
import ink.xiaobaibai.service.IBuyOzoneDeviceService;
import ink.xiaobaibai.service.IFreeOzoneDeviceService;
import ink.xiaobaibai.service.IMonthlyfeeDeviceService;
import ink.xiaobaibai.service.IOzoneDeviceService;
import ink.xiaobaibai.serviceOxy.emqtt.IBaseEMqttService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description: 机器通信层
 * @author: 小白白
 * @create: 2021-08-08
 **/

@Service
public class MqttServiceImpl implements IMqttService {

    @Autowired
    private IBaseEMqttService baseEMqttService;

    @Autowired
    private ISmsService smsService;

    @Autowired
    private IOzoneDeviceService ozoneDeviceService;

    @Autowired
    private IMonthlyfeeDeviceService monthlyfeeDeviceService;

    @Autowired
    private IBuyOzoneDeviceService buyOzoneDeviceService;

    @Autowired
    private IFreeOzoneDeviceService freeOzoneDeviceService;

    @Override
    public boolean checkOnePayDevice(String deviceCode) {
        boolean check = this.baseEMqttService.check(deviceCode);
        if (!check) {
            this.smsService.deviceFaultSms(deviceCode, DeviceType.ONEPAY, false);
            return false;
        }
        // 通过臭氧deviceCode查询对应氧气机的code
        String oxygenCodeByDeviceCode = this.ozoneDeviceService.getOxygenCodeByDeviceCode(deviceCode);
        check = this.baseEMqttService.check(oxygenCodeByDeviceCode);
        if (!check) {
            this.smsService.deviceFaultSms(deviceCode, DeviceType.ONEPAY, true);
            return false;
        }
        return true;
    }

    @Override
    public boolean checkDeviceByType(String deviceCode, DeviceType deviceType) {

        if (deviceType == DeviceType.MON) {
            boolean check = this.baseEMqttService.check(deviceCode);
            if (!check) {
                this.smsService.deviceFaultSms(deviceCode, deviceType, false);
                return false;
            }
            // 通过臭氧deviceCode查询对应氧气机的code
            String oxygenCodeByDeviceCode = this.monthlyfeeDeviceService.getOxygenCodeByDeviceCode(deviceCode);
            check = this.baseEMqttService.check(oxygenCodeByDeviceCode);
            if (!check) {
                this.smsService.deviceFaultSms(deviceCode, deviceType, true);
                return false;
            }
            return true;
        } else if (deviceType == DeviceType.BUY) {
            boolean check = this.baseEMqttService.check(deviceCode);
            if (!check) {
                this.smsService.deviceFaultSms(deviceCode, deviceType, false);
                return false;
            }
            // 通过臭氧deviceCode查询对应氧气机的code
            String oxygenCodeByDeviceCode = this.buyOzoneDeviceService.getOxygenCodeByDeviceCode(deviceCode);
            check = this.baseEMqttService.check(oxygenCodeByDeviceCode);
            if (!check) {
                this.smsService.deviceFaultSms(deviceCode, deviceType, true);
                return false;
            }
            return true;
        } else {
            boolean check = this.baseEMqttService.check(deviceCode);
            if (!check) {
                this.smsService.deviceFaultSms(deviceCode, deviceType, false);
                return false;
            }
            // 通过臭氧deviceCode查询对应氧气机的code
            String oxygenCodeByDeviceCode = this.freeOzoneDeviceService.getOxygenCodeByDeviceCode(deviceCode);
            check = this.baseEMqttService.check(oxygenCodeByDeviceCode);
            if (!check) {
                this.smsService.deviceFaultSms(deviceCode, deviceType, true);
                return false;
            }
            return true;
        }
    }

    @Override
    public void openDevice(String deviceCode, DeviceMqttBo deviceMqttBo) {
        this.baseEMqttService.pushMsg(deviceCode, deviceMqttBo);
    }

    @Override
    public void closeDevice(String deviceCode) {
        this.baseEMqttService.pushMsg(deviceCode, new DeviceMqttBo(deviceCode, false));
    }

    @Override
    public void closeDevice(String deviceCode, String roomNumber) {
        this.baseEMqttService.pushMsg(deviceCode, new DeviceMqttBo(deviceCode, false, roomNumber));
    }

}
